// pages/myNoteDetail/myNoteDetail.js
const app = getApp();
import api from '../../utils/api.js';
let arr = []

Page({

  /**
   * 页面的初始数据
   */
  data: {
    questions: [],
    currentIndex: 0,
    // isCollect: false,
    isShowNote: false,
    isShowtab: false,
    isShowFeedback: false,

    errOptions: [
      {
        type: '图文不符',
        isSelect: false
      },
      {
        type: '有错别字',
        isSelect: false
      },
      {
        type: '符号乱码',
        isSelect: false
      },
      {
        type: '内容有误',
        isSelect: false
      },
      {
        type: '图片模糊',
        isSelect: false
      }],
    currentErrIndex: 0,
    finishedQuestion: []
  },

  

  chooseAnswer(e) {
    if (this.data.questions[this.data.currentIndex].isChoice) {

      if (this.data.questions[this.data.currentIndex].true_answer.length > 1) {
        // 多选题
        if (arr.indexOf(e.currentTarget.dataset.index) == -1) {
          arr.push(e.currentTarget.dataset.index)
        } else {
          let index = arr.indexOf(e.currentTarget.dataset.index)
          arr.splice(index, 1)
        }
        arr.sort(function (a, b) {
          return a - b
        })
        console.log(arr)
        this.data.questions[this.data.currentIndex].yourIndex = arr

        this.setData({
          questions: this.data.questions
        })
      } else {
        let yourIndex = e.currentTarget.dataset.index
        let yourAnswer = this.data.questions[this.data.currentIndex].options[yourIndex].slice(0, 1)
        let trueAnswer = this.data.questions[this.data.currentIndex].true_answer

        if (yourAnswer != trueAnswer) {
          this.data.questions[this.data.currentIndex].errAnswerIndex = yourAnswer == 'A' ? 0 : yourAnswer == 'B' ? 1 : yourAnswer == 'C' ? 2 : yourAnswer == 'D' ? 3 : ''
          this.data.questions[this.data.currentIndex].is_true = false
        }else{
          this.data.questions[this.data.currentIndex].is_true = true
        }

        api.POST('applet/customerSubject/create', {
          subject_id: this.data.questions[this.data.currentIndex].subject_id,
          answer: yourAnswer,
          type: 1
        }, res => {
          console.log(res)
        })

        this.setData({
          yourAnswer
        })

        // 显示正确答案
        let true_answer = this.data.questions[this.data.currentIndex].true_answer
        this.data.questions[this.data.currentIndex].trueAnswerIndex = true_answer == 'A' ? 0 : true_answer == 'B' ? 1 : true_answer == 'C' ? 2 : true_answer == 'D' ? 3 : ''
        // 显示解析
        let isShowDesc = 'questions[' + this.data.currentIndex + '].isShowDesc'
        this.setData({
          [isShowDesc]: true,
        })

        this.data.questions[this.data.currentIndex].isChoice = false
        this.data.finishedQuestion.push(this.data.currentIndex)
        this.setData({
          questions: this.data.questions,
          finishedQuestion: this.data.finishedQuestion
        })

      }


    }

  },
  // 确认多选题答案
  confirmAnswer() {


    // 你的答案 ['C','A']
    let answerArr = arr.map(item => {
      return this.data.questions[this.data.currentIndex].options[item].slice(0, 1)
    })
    console.log(answerArr)
    this.data.questions[this.data.currentIndex].answerArr = answerArr
    this.data.questions[this.data.currentIndex].answer = answerArr.join('|')
    console.log(this.data.questions[this.data.currentIndex].answer)

    api.POST('applet/customerSubject/create', {
      subject_id: this.data.questions[this.data.currentIndex].subject_id,
      answer: this.data.questions[this.data.currentIndex].answer,
      type: 1
    }, res => {
      console.log(res)
    })

    //正确答案
    let trueAnswerArr = this.data.questions[this.data.currentIndex].true_answer.split('|').sort()
    this.data.questions[this.data.currentIndex].trueAnswerArr = trueAnswerArr
    this.data.questions[this.data.currentIndex].trueAnswerSort = trueAnswerArr.join('|')

    let trueAnswerIndex = trueAnswerArr.map(item => {
      return item == 'A' ? 0 : item == 'B' ? 1 : item == 'C' ? 2 : item == 'D' ? 3 : item == 'E' ? 4 : item == 'F' ? 5 : item == 'G' ? 6 : ''
    })
    this.data.questions[this.data.currentIndex].trueAnswerIndex = trueAnswerIndex
    this.setData({
      questions: this.data.questions
    })
    // 你的答案与正确答案的比较
    let yourAnswerIndex = this.data.questions[this.data.currentIndex].yourIndex
    let errAnswerArr = []
    let missedArr = []

    if (yourAnswerIndex.length < trueAnswerIndex.length) {
      yourAnswerIndex.map(item => {
        if (trueAnswerIndex.indexOf(item) != -1) {
          trueAnswerIndex.map(item1 => {
            if (yourAnswerIndex.indexOf(item1) == -1) {
              missedArr.push(item1)
            }
            this.data.questions[this.data.currentIndex].missedAnswerIndex = missedArr
          })
        } else {
          errAnswerArr.push(item)
          this.data.questions[this.data.currentIndex].errAnswerIndex = errAnswerArr
        }
      })
    } else {
      yourAnswerIndex.map(item => {
        if (trueAnswerIndex.indexOf(item) == -1) {
          errAnswerArr.push(item)
        }
        this.data.questions[this.data.currentIndex].errAnswerIndex = errAnswerArr
      })
    }

    if (this.data.questions[this.data.currentIndex].answer === this.data.questions[this.data.currentIndex].trueAnswerSort){
      this.data.questions[this.data.currentIndex].is_true = true
    }else{
      this.data.questions[this.data.currentIndex].is_true = false
    }

    this.data.questions[this.data.currentIndex].yourIndex = []
    this.data.questions[this.data.currentIndex].isChoice = false
    this.data.questions[this.data.currentIndex].isShowDesc = true
    this.setData({
      questions: this.data.questions
    })

    arr = []
  },

  // 切换题目
  start(e) {
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY
    })
  },

  end(e) {
    let endX = e.changedTouches[0].clientX;
    let endY = e.changedTouches[0].clientY;

    if (endX - this.data.startX > 50 && Math.abs(endY - this.data.startY) < 50) {
      // 右滑
      if (this.data.currentIndex > 0) {
        this.data.currentIndex--;
        this.setData({
          currentIndex: this.data.currentIndex
        })
      } else {
        return
      }
    } else if (this.data.startX - endX > 50 && Math.abs(endY - this.data.startY) < 50) {
      // 左滑
      if (this.data.currentIndex < this.data.questions.length - 1) {
        this.data.currentIndex++;
        this.setData({
          currentIndex: this.data.currentIndex
        })
      } else {
        wx.showToast({
          title: '已经是最后一题了',
          icon: "loading"
        })
      }
    }
  },

  // 点击按钮进入上一题
  preQues() {
    if (this.data.currentIndex > 0) {
      this.data.currentIndex--;
      this.setData({
        currentIndex: this.data.currentIndex
      })
    } else {
      return
    }
  },

  // 点击按钮进入下一题
  nextQues() {
    if (this.data.currentIndex < this.data.questions.length - 1) {
      this.data.currentIndex++;
      this.setData({
        currentIndex: this.data.currentIndex
      })
    } else {
      wx.showToast({
        title: '已经是最后一题了',
        icon: "none"
      })
    }
  },


  // 修改收藏状态
  collect() {
    this.data.questions[this.data.currentIndex].status = !this.data.questions[this.data.currentIndex].status

    this.setData({
      questions: this.data.questions
    })

    api.POST('applet/SubjectFavorite/create', {
      subject_id: this.data.questions[this.data.currentIndex].subject_id
    }, res => {
      console.log(res)
    })
  },
  // 添加修改笔记
  showNote() {
    this.setData({
      isShowNote: true
    })

    api.POST('applet/SubjectNote/detail', {
      subject_id: this.data.questions[this.data.currentIndex].subject_id
    }, res => {
      console.log(res)
      if (res.data.content) {
        this.data.questions[this.data.currentIndex].content = res.data.content
        this.setData({
          questions: this.data.questions
        })
      }
    })
  },
  closeNote() {
    this.setData({
      isShowNote: false
    })
  },
  addNote(e) {
    console.log(this.data.questions[this.data.currentIndex].content)

    if (this.data.questions[this.data.currentIndex].content != null) {
      this.setData({
        questions: this.data.questions,
        isShowNote: false
      })
      wx.showToast({
        title: '成功添加笔记',
      })
      api.POST('applet/SubjectNote/edit', {
        subject_id: this.data.questions[this.data.currentIndex].subject_id,
        content: e.detail.value.content
      }, res => {
        console.log(res)
      })

      this.data.questions[this.data.currentIndex].content = !this.data.questions[this.data.currentIndex].content
      console.log(this.data.questions[this.data.currentIndex].content)

    } else {
      wx.showToast({
        title: '添加内容不能为空',
        icon: 'none'
      })
      this.setData({
        isShowNote: false
      })
    }

  },

  // 答题卡
  showTab() {
    this.setData({
      isShowtab: true
    })
  },
  closeTab() {
    this.setData({
      isShowtab: false
    })
  },
  changeCurrentIndex(e) {
    this.setData({
      currentIndex: e.currentTarget.dataset.index
    })
  },
  redo() {
    api.POST('applet/SubjectNote/findAll', {
      paper_id: this.data.paper_id
    }, res => {
      console.log(res)
      this.setData({
        questions: res.data.data,
        currentIndex: 0
      })
      let arr = this.data.questions.map((item, index) => {
        item.isChoice = true
        item.name = index + 1 + item.name
        return item
      })
      this.setData({
        questions: this.data.questions
      })
    })
  },

  // 纠错相关
  showFeedback() {
    this.setData({
      isShowFeedback: true
    })
  },
  closeFeedback() {
    this.setData({
      isShowFeedback: false
    })
  },
  selectErrTab(e) {
    this.data.errOptions[e.currentTarget.dataset.index].isSelect = !this.data.errOptions[e.currentTarget.dataset.index].isSelect
    this.setData({
      // currentErrIndex: e.currentTarget.dataset.index
      errOptions: this.data.errOptions
    })
  },
  submitFeedback(e) {
    let arr = []
    for (let i = 0; i < this.data.errOptions.length; i++) {
      if (this.data.errOptions[i].isSelect) {
        arr.push(this.data.errOptions[i].type)
      }
    }

    api.POST('applet/FeedBack/create', {
      type: arr.join('、'),
      subject_id: this.data.questions[this.data.currentIndex].subject_id,
      describe: e.detail.value.feedbackContent || ""
    }, res => {
      console.log(res)
    })

    for (let i = 0; i < this.data.errOptions.length; i++) {
      this.data.errOptions[i].isSelect = false
    }
    this.setData({
      isShowFeedback: false,
      errOptions: this.data.errOptions
    })
    wx.showToast({
      title: '提交成功',
    })
  },

  getPaperQuestion() {
    api.POST('applet/SubjectNote/findAll', {
      paper_id: this.data.paper_id
    },
      res => {
        console.log(res)
        this.setData({
          questions: res.data.data,
          totalNum: res.data.data.length
        })
        for (let i = 0; i < this.data.questions.length; i++) {
          this.data.questions[i].isChoice = true
          // this.data.questions[i].status = false
          this.data.questions[i].noteContent = ''
          this.data.questions[i].name = i + 1 + this.data.questions[i].name
          this.data.questions[i].errOptions = this.data.errOptions
        }
        this.setData({
          questions:this.data.questions
        })
        console.log(this.data.questions)
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type:options.type,
      paper_id: options.paper_id,
      windowHeight: app.globalData.windowHeight

    })
    wx,wx.setNavigationBarTitle({
      title: options.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPaperQuestion()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})